package controller;

import dao.UserDao;
import utils.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");
        String method=request.getParameter("method");
        if(method.equalsIgnoreCase("add"))
            add(request, response);
        else
        if(method.equalsIgnoreCase("delete"))
            delete(request, response);
        else
        if(method.equalsIgnoreCase("update"))
            update(request, response);
        else
        if(method.equalsIgnoreCase("search"))
            search(request, response);
        else
        if(method.equalsIgnoreCase("searchById"))
            searchById(request, response);
    }

    //增加用户
    private void add(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("add()方法...");
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String tel = req.getParameter("tel");
        String address = req.getParameter("address");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setSex(sex);
        user.setTel(tel);
        user.setAddress(address);
        System.out.println(user);
        UserDao userDao = new UserDao();
        boolean tag = userDao.insert(user);
        if(tag){
            System.out.println("添加成功...");
            req.getRequestDispatcher("/user/index.jsp").forward(req,resp);
        }else{
            System.out.println("添加失败...");
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("userid");
        UserDao userDao = new UserDao();
        boolean delete = userDao.delete(Integer.parseInt(userid));
        System.out.println("userid="+userid+"       delete:"+delete);
        if(delete){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        search(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {

    }

    private void search(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("search方法...");
        UserDao userDao = new UserDao();
        ArrayList<User> users = userDao.findAll();
        request.setAttribute("allUser",users);
        try
        {
            request.getRequestDispatcher("/user/index.jsp").forward(request, response);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private void searchById(HttpServletRequest request, HttpServletResponse response) {

    }








}
